package com.xintoucloud.gsv.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xintoucloud.gsv.entity.Dept;
import com.xintoucloud.gsv.entity.SuperVision;
import com.xintoucloud.gsv.entity.SuperVisionState;
import com.xintoucloud.gsv.entity.SupervisionType;
import com.xintoucloud.gsv.model.DataGrid;
import com.xintoucloud.gsv.model.SuperVisionDto;
import com.xintoucloud.gsv.service.DeptService;
import com.xintoucloud.gsv.service.SuperVisionService;
import com.xintoucloud.gsv.service.SupervisionTypeService;

@Controller
public class SuperVisionManage {
	@Autowired
	private SupervisionTypeService supervisionTypeService;
	
	@Autowired
	private SuperVisionService superVisionService;
	
	@Autowired
	private DeptService deptTypeService;
	
	@RequestMapping("/showSuperVisionManage")
	@ResponseBody
	public DataGrid<SuperVisionDto>  showSuperVisionManage(Model m,HttpServletRequest request) {
		//1.获取参数
		 String page = request.getParameter("page");// 获取当前页 
		 String size = request.getParameter("limit");// 获取每页信息显示的条数 
		 //2.数据库执行分页操作
		DataGrid<SuperVisionDto> superVisions=superVisionService.listPage(Integer.parseInt(page), Integer.parseInt(size));
		//System.out.println("count:"+superVisions.getCount());
		m.addAttribute("superVisions", superVisions);
		return superVisions;
	}
	@RequestMapping("/toSuperVisionManage")
	public String toSuperVisionManage() {
		return "superVisionManage";
	}
	@RequestMapping("/toSuperVisionDraft")
	public String toSuperVisionDraft(Model m) {
		List<Dept> depts=deptTypeService.listDept();
		m.addAttribute("depts", depts);
		List<SupervisionType> superVisionTypes=supervisionTypeService.list();
		m.addAttribute("superVisionTypes", superVisionTypes);
		return "superVisionDraft";
	}
	@RequestMapping("/showSuperVisionDraft")
	@ResponseBody
	public DataGrid<SuperVisionDto> showSuperVisionDraft(Model m,HttpServletRequest request) {
		//1.获取参数
		 String page = request.getParameter("page");// 获取当前页 
		 String size = request.getParameter("limit");// 获取每页信息显示的条数 
		 //2.数据库执行分页操作
		DataGrid<SuperVisionDto> superVisions=superVisionService.listDraftPage(Integer.parseInt(page), Integer.parseInt(size));
		//System.out.println("count:"+superVisions.getCount());
		m.addAttribute("superVisions", superVisions);
		return superVisions;
	}
/*	@PostMapping("/superVisionDelete")
	@ResponseBody
	public SuperVision superVisionDelete(int svid) {
		superVisionService.deleteSuperVision(svid);
		SuperVision superVision= superVisionService.getSuperVision(svid);
		return superVision;
		
	}*/

	@PostMapping("/superVisionDelete")
	@ResponseBody
	public Map<String, Object> superVisionDelete(int svid) {
		superVisionService.deleteSuperVision(svid);
		Map<String,Object> map = new HashMap<String,Object>();
		SuperVision superVision= superVisionService.getSuperVision(svid);
		if(superVision == null) {
			map.put("msg", "恭喜您,删除成功!");
		}else {
			map.put("msg", "很遗憾,删除失败!");
		}
		return map;
	}
	
	@PostMapping("/superVisionStart")
	@ResponseBody
	public SuperVision superVisionStart(Integer svid,Integer sid,Integer did) {
		System.out.println("发起督办执行了");
		SuperVision superVision= superVisionService.getSuperVision(svid);
		SuperVisionState superVisionState=new SuperVisionState();
		superVisionState.setStid(2);
		superVision.setSuperVisionState(superVisionState);
		SupervisionType supervisionType=new SupervisionType();
		supervisionType.setSid(sid);
		superVision.setSupervisionType(supervisionType);
		Dept dept=new Dept();
		dept.setDid(did);
		superVision.setDept(dept);
		Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        superVision.setStartTime(sdf.format(date));
		superVisionService.update(superVision);
		SuperVision superVision1= superVisionService.getSuperVision(svid);
		return superVision1;
		
	}
}
